package logic

import (
	"context"
	"errors"
	"pro-train-hub/models/mysql"

	"pro-train-hub/trainrpc/internal/svc"
	"pro-train-hub/trainrpc/trainrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectLogic {
	return &CollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectLogic) Collect(in *trainrpc.CollectRequest) (*trainrpc.CollectResponse, error) {
	// todo: add your logic here and delete this line
	var coll mysql.Collect
	mysql.DB.Debug().Where("user_id = ? and lesson_id = ?", in.UserId, in.LessonId).Find(&coll)
	if coll.ID != 0 {
		return nil, errors.New("该课程已被收藏")
	}
	coll = mysql.Collect{
		UserId:   in.UserId,
		LessonId: in.LessonId,
	}
	mysql.DB.Debug().Create(&coll)
	return &trainrpc.CollectResponse{
		CollectId: int64(coll.ID),
	}, nil
}
